package node;

import java.rmi.Naming;
import java.rmi.RemoteException;


public class RmiClientNodeStorage implements NodeStorage{
	
	private String rmiUrl;
	
	public RmiClientNodeStorage(String rmiUrl){
		System.out.println("RmiClientNodeStorage url: " + rmiUrl);
		this.rmiUrl=rmiUrl;
	}
	
	private RmiNodeStorage getClient(){
		try {
			
			RmiNodeStorage client = (RmiNodeStorage) Naming.lookup(rmiUrl);
			return client;
		} catch (Exception e) {
			System.out.println("NodeStorage Client error:" + e.getMessage());
			e.printStackTrace();
		}return null;
	}
	
	@Override
	public Node read() throws NodeStorageException{
		try {
			System.out.println("Read from client:");
			return getClient().read();
		} catch (RemoteException e) {
			System.out.println("Read error: " + e.getMessage());
			e.printStackTrace();
		}return null;
	}
	
	
	@Override
	public void store(Node node) throws NodeStorageException{
		try {
			System.out.println("Store from client:");
			getClient().store(node);
		} catch (RemoteException e) {
			System.out.println("Store error:" + e.getMessage());
			e.printStackTrace();
		}
	}

	
}
	
